package service

import (
	"gorm.io/gorm"
	"gowork/microSignalSMSSending/dao"
	"gowork/microSignalSMSSending/dataInit"
)

type AdviserServiceImf struct {
	Tx *gorm.DB
	AdviserDaoImf *dao.AdviserDaoImf
	DistributionRecordsImf *dao.DistributionRecordsImf
	SendingModeRecordDaoImf *dao.SendingModeRecordDaoImf
	SMSRecordDaoImf *dao.SMSRecordDaoImf

}

func (adviserServiceImf *AdviserServiceImf) GetWhetherToPopUp(account string, customerId int64) (map[string]interface{}, error){
	adviserServiceImf.Tx = dataInit.Db.Begin()
	adviserServiceImf.AdviserDaoImf = new(dao.AdviserDaoImf)
	adviserServiceImf.DistributionRecordsImf = new(dao.DistributionRecordsImf)
	adviserServiceImf.SendingModeRecordDaoImf = new(dao.SendingModeRecordDaoImf)
	adviserServiceImf.SMSRecordDaoImf = new(dao.SMSRecordDaoImf)

	isLeader, err := adviserServiceImf.AdviserDaoImf.GetAccountIsLeader(account, adviserServiceImf.Tx)
	if err != nil{
		adviserServiceImf.Tx.Rollback()
	}

	allocateTime, err := adviserServiceImf.DistributionRecordsImf.GetAllocateTimeByCustomerId(customerId, adviserServiceImf.Tx)
	if err != nil{
		adviserServiceImf.Tx.Rollback()
	}

	smrCount, err := adviserServiceImf.SendingModeRecordDaoImf.GetSendingModeRecordByCustomerId(customerId, adviserServiceImf.Tx)
	if err != nil{
		adviserServiceImf.Tx.Rollback()
	}

	smsrCount, err := adviserServiceImf.SMSRecordDaoImf.GetSMSRecordByCustomerId(customerId, allocateTime, 2, adviserServiceImf.Tx)
	if err != nil{
		adviserServiceImf.Tx.Rollback()
	}

	Popup := make(map[string]interface{})
	if isLeader == false && smrCount == 0 && smsrCount == 0{
		Popup["popUpOrNot"] = true
	}else {
		Popup["popUpOrNot"] = false
	}

	adviserServiceImf.Tx.Commit()

	return Popup, nil
}
